<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="80px" style="display: flex; flex-wrap: nowrap;">
      <el-form-item label="客户名称:" prop="f0302" style="display: flex; flex-wrap: nowrap;">
        <el-input
          style="width: 14vw"
          v-model="queryParams.f0302"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin: 0 0 0 50vw;">
        <el-button type="primary" icon="el-icon-search" @click="handleQuery" >搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery" >重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['t03:t03:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          style="background-color: black; color: white;"
          @click="tokenzs"
          v-hasPermi="['t03:t03:export']"
        >token赠送</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="t03List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="f0302" />
      <el-table-column label="系统名" align="center" prop="f0303" />
      <el-table-column label="系统账号" align="center" prop="f0304" />
      <el-table-column label="查询价格" align="center" prop="f0306" />
      <el-table-column label="剩余token" align="center" prop="f0310" />
      <el-table-column label="联系电话" align="center" prop="f0309" />
      <el-table-column label="系统状态" align="center" prop="f0314">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xtzt" :value="scope.row.f0314"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['t03:t03:edit']"
          >修改</el-button>
          <el-button size="mini" v-if="scope.row.f0314 == 1"
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['t03:t03:edit']"
            style="color: #b71c1c;"
          >禁用</el-button>
          <el-button size="mini" v-else
            type="text"
            @click="changeStatus(scope.row)"
            v-hasPermi="['t03:t03:edit']"
          >启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加客户管理对话框 -->
  <el-dialog :title="title" :visible.sync="openadd" width="55vw" >
    <el-form ref="form" :model="form" :rules="rules" label-width="6vw"
    style="padding: 0 0 0 2vw" >
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="客户名称" prop="f0302">
            <el-input v-model="form.f0302" placeholder="请输入客户名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="11" class="input-col">
          <el-form-item label="系统名" prop="f0303">
            <el-input v-model="form.f0303" placeholder="请输入系统名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="账号" prop="f0304">
            <el-input v-model="form.f0304" placeholder="请输入账号" />
          </el-form-item>
        </el-col>
        <el-col :span="11" class="input-col">
          <el-form-item label="密码" prop="f0305">
            <el-input v-model="form.f0305" placeholder="请输入密码" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="查询价格" prop="f0306">
            <el-input v-model="form.f0306" placeholder="请输入查询价格" />
          </el-form-item>
        </el-col>
        <el-col :span="11" class="input-col">
          <el-form-item label="试用token" prop="f0307">
            <el-input v-model="form.f0307" placeholder="请输入试用token" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="联系电话" prop="f0309">
            <el-input v-model="form.f0309" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="padding: 0 6vw 0 0;
    margin: -2vw 0 4vw 0;">
      <el-button @click="canceladd">取消</el-button>
      <el-button type="primary" @click="submitForm" >保存</el-button>
      <el-button style="background-color: black; color: white;" @click="submitFormJX">保存并继续添加</el-button>
    </div>
  </el-dialog>
    <!-- 修改客户管理对话框 -->
  <el-dialog :title="title" :visible.sync="openupdate" width="55vw" height="55vw" >
    <el-form ref="form" :model="form" :rules="rules" label-width="5vw" style="padding: 0 0 0 2vw">
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="客户名称" prop="f0302">
            <el-input v-model="form.f0302" placeholder="请输入客户名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="11" class="input-col">
          <el-form-item label="系统名" prop="f0303">
            <el-input v-model="form.f0303" placeholder="请输入系统名"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="账号" prop="f0304">
            <el-input v-model="form.f0304" placeholder="请输入账号" />
          </el-form-item>
        </el-col>
        <el-col :span="11" class="input-col">
          <el-form-item label="密码" prop="f0305">
            <el-input v-model="form.f0305" placeholder="请输入密码" type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="查询价格" prop="f0306">
            <el-input v-model="form.f0306" placeholder="请输入查询价格" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="input-row">
        <el-col :span="11" class="input-col">
          <el-form-item label="联系电话" prop="f0309">
            <el-input v-model="form.f0309" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="padding: 0 6vw 0 0;">
        <el-button @click="cancelupdate">取 消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
  </el-dialog>
  <!-- tokenzs token赠送 -->
   <el-dialog :title="title" :visible.sync="opentokenzs"  width="33vw" :appendToBody="true">
    <el-form ref="tokenform" :model="tokenform" :rules="rules" label-width="5vw" >
      <el-row :gutter="15" class="input-row">
          <el-form-item label="赠送客户:" prop="field108" label-width="6vw">
            <el-select v-model="tokenform.F0301"  placeholder="下拉选择" clearable :style="{width: '100%'}">
              <el-option
                v-for="item in tokenaselect"
                :key="item.key"
                :label="item.values"
                :value="item.key">
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="15" class="input-row">
          <el-form-item label="赠送token:" prop="field102" label-width="6vw">
            <el-input v-model="tokenform.F0310" placeholder="请输入赠送token" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="padding: 0 2vw 0 0;">
      <el-button @click="canceltokienzs">取 消</el-button>
      <el-button type="primary" @click="submittokenzs">保存</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import { listT03, getT03, addT03, updateT03 } from "@/api/t03/t03";
import request from "@/utils/request.js"
export default {
  name: "T03",
  dicts: ['xtzt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      t03List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层增加
      openadd: false,
      // 是否显示弹出层修改
      openupdate: false,
      //token赠送控制弹窗的显示状态
      opentokenzs:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        f0302: null,
      },
      // 表单参数
      form: {},
      // token赠送的参数
      tokenform:{},
      // 下拉选项的数据源
      tokenaselect:[
      ],
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tokenreset(){
      this.tokenform = {
        F0301:null,
        F0310:null,
      }
    },
    // 表单重置
    reset() {
      this.form = {
        f0301: null,
        f0302: null,
        f0303: null,
        f0304: null,
        f0305: null,
        f0306: null,
        f0307: null,
        f0310: null,
        f1801: null,
        f0309: null,
        f0314: null,
        f0311: null,
        f0312: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.f0301)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.f0301 != null) {
            updateT03(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openupdate = false;
              this.getList();
            });
          } else {
            addT03(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openadd = false;
              this.getList();
            });
          }
        }
      });
    },

// 搜索
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listT03(this.queryParams).then(response => {
        this.t03List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

//修改相关
    /** 根据传来的值对客户的状态进行修改 */
    changeStatus(row) {
      const a = row.f0301;
      const b = row.f0314;
      if (b === 1) {
        updateT03({ f0301: a, f0314: 0 }).then(response => {
          this.$modal.msgSuccess("禁用成功");
          this.getList();
        });
      } else {
        updateT03({f0301: a, f0314: 1 }).then(response=>{
          this.$modal.msgSuccess("启用成功");
          this.getList();
        })
      }
    },
    /** 修改按钮 */
    handleUpdate(row) {
      this.reset();
      const f0301 = row.f0301 || this.ids
      getT03(f0301).then(response => {
        this.form = response.data;
        this.openupdate = true;
        this.title = "修改客户管理";
      });
    },
    // 取消按钮
    cancelupdate() {
      this.openupdate = false;
      this.reset();
    },

//新增相关
    /** 新增按钮 */
    handleAdd() {
      this.reset();
      this.openadd = true;
      this.title = "添加客户管理";
    },
    // 取消按钮
    canceladd() {
      this.openadd = false;
      this.reset();
    },
    /**保持并继续提交 */
    submitFormJX() {
      this.$refs["form"].validate(valid => {
        addT03(this.form).then(response => {
              this.reset();
              this.$modal.msgSuccess("新增成功");
              this.getList();
              /**将表单中的内容清空 */
            });
      });
    },

//token赠送
    /**token赠送按钮 */
    tokenzs() {
    this.tokenreset();
    //this.tokenaselect 中key为this.t03List的f0301 values为this.t03List的f0302
    this.tokenaselect = this.t03List.map(item => {
      return {
        key: item.f0301,
        values: item.f0302
      };
    });
    this.title = "token赠送";
    this.opentokenzs = true;
   },
    //token赠送token取消按钮
    canceltokienzs(){
      this.opentokenzs = false;
      this.reset();
    },
    /**token赠送提交按钮 */
    submittokenzs(){
      this.$refs["tokenform"].validate(valid => {
        if (valid) {
          request.put('/t03/t03/tokenGifting', {
              f0301:this.tokenform.F0301,
              f0310:parseInt(this.tokenform.F0310)
            }).then(response=>{
              if(response.code===200) {
                this.$modal.msgSuccess("赠送成功");
              }
              else {
                this.$modal.msgError(response.msg);
              }
              this.opentokenzs = false;
              this.getList();
          })
         }
      })
    }
  }
};
</script>
<style>
/*绿*/
.el-tag-common-green {
    background-color: #E8FFEA; /* 浅绿色背景 */
    border-color: #98e69e; /* 深一些的绿色边框 */
    color: #66ffcc; /* 更亮的绿色字体 */
    display: inline-flex;
    justify-content: center;
    align-items: center;
    height: 24px;
    padding: 0 9px;
    font-size: 12px;
    line-height: 1;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
}
.el-tag-green {
    /* 假设您希望.el-tag-green与.el-tag-common完全一致，或者您可以指定不同的绿色 */
    background-color: #e0f2f1; /* 浅绿色背景 */
    border-color: #b2d7d2; /* 深一些的绿色边框 */
    color: #4fdc4f; /* 更亮的绿色字体 */
    /* 如果需要特别指定.el-tag-green独有的样式，可以在这里添加 */
}

/* 红 */
.el-tag-common-red {
    background-color: #ffebee; /* 浅红色背景 */
    border-color: #d32f2f; /* 深红色边框 */
    color: #b71c1c; /* 深红色字体 */
    display: inline-flex;
    justify-content: center;
    align-items: center;
    height: 24px;
    padding: 0 9px;
    font-size: 12px;
    line-height: 1;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
}
.el-tag-red {
    /* 重写.el-tag-common的样式以使用红色主题 */
    background-color: #ffebee; /* 浅红色背景 */
    border-color: #d32f2f; /* 深红色边框 */
    color: #b71c1c; /* 深红色字体 */
    /* 如果需要特别指定.el-tag-green独有的样式，可以在这里添加 */
}

.dialog-custom-class {
  width: 885px;
  height: 497px;
}

.form-custom-class {
  width: 100%;
  padding: 20px;
}
</style>
